using System;
using System.Collections.Generic;
using UnityEngine;

namespace GenerativeAgents.Core
{
    /// <summary>
    /// 智能体事件数据结构
    /// 表示智能体感知到的环境事件，包含完整的事件信息和元数据
    /// </summary>
    [System.Serializable]
    public class AgentEvent
    {
        [Header("基础事件信息")]
        public string eventId;              // 唯一事件ID
        public string subject;              // 主语（谁）
        public string predicate;            // 谓语（做什么）
        public string objectEntity;         // 宾语（对什么）
        public string description;          // 详细描述
        
        [Header("时空信息")]
        public Vector3 location;            // 发生位置
        public DateTime timestamp;          // 时间戳
        public string locationName;         // 位置名称
        
        [Header("重要性和语义信息")]
        public int poignancy;              // 重要性分数(1-10)
        public List<string> keywords;      // 关键词列表
        public float[] embedding;          // 语义嵌入向量
        
        [Header("参与者信息")]
        public List<string> participants;  // 参与者列表
        public bool isConversation;        // 是否为对话事件
        public string conversationId;      // 对话ID（如果是对话）
        
        [Header("事件分类")]
        public EventType eventType;        // 事件类型
        public EventPriority priority;     // 事件优先级
        
        [Header("额外元数据")]
        public Dictionary<string, object> metadata; // 额外的元数据
        
        public AgentEvent()
        {
            eventId = System.Guid.NewGuid().ToString();
            timestamp = DateTime.Now;
            keywords = new List<string>();
            participants = new List<string>();
            metadata = new Dictionary<string, object>();
            poignancy = 1;
            priority = EventPriority.Normal;
        }
        
        /// <summary>
        /// 创建移动事件
        /// </summary>
        public static AgentEvent CreateMoveEvent(string agentName, Vector3 from, Vector3 to, string locationName)
        {
            var moveEvent = new AgentEvent
            {
                subject = agentName,
                predicate = "moved to",
                objectEntity = locationName,
                description = $"{agentName} moved from {from} to {to} at {locationName}",
                location = to,
                eventType = EventType.Movement,
                participants = new List<string> { agentName }
            };
            
            moveEvent.keywords.AddRange(new[] { "movement", "location", agentName.ToLower(), locationName.ToLower() });
            return moveEvent;
        }
        
        /// <summary>
        /// 创建对话事件
        /// </summary>
        public static AgentEvent CreateConversationEvent(string speaker, string listener, string content, Vector3 location, string conversationId)
        {
            var chatEvent = new AgentEvent
            {
                subject = speaker,
                predicate = "said to",
                objectEntity = $"{listener}: \"{content}\"",
                description = $"{speaker} said to {listener}: \"{content}\"",
                location = location,
                eventType = EventType.Conversation,
                isConversation = true,
                conversationId = conversationId,
                participants = new List<string> { speaker, listener }
            };
            
            chatEvent.keywords.AddRange(new[] { "conversation", "dialogue", speaker.ToLower(), listener.ToLower() });
            return chatEvent;
        }
        
        /// <summary>
        /// 创建观察事件
        /// </summary>
        public static AgentEvent CreateObservationEvent(string observer, string observed, string action, Vector3 location)
        {
            var observeEvent = new AgentEvent
            {
                subject = observer,
                predicate = "observed",
                objectEntity = $"{observed} {action}",
                description = $"{observer} observed {observed} {action}",
                location = location,
                eventType = EventType.Observation,
                participants = new List<string> { observer, observed }
            };
            
            observeEvent.keywords.AddRange(new[] { "observation", observer.ToLower(), observed.ToLower(), action.ToLower() });
            return observeEvent;
        }
    }
    
    /// <summary>
    /// 事件类型枚举
    /// </summary>
    public enum EventType
    {
        Movement,       // 移动事件
        Conversation,   // 对话事件
        Observation,    // 观察事件
        Interaction,    // 交互事件
        Planning,       // 计划事件
        Reflection,     // 反思事件
        Action,         // 行动事件
        Environment     // 环境事件
    }
    
    /// <summary>
    /// 事件优先级
    /// </summary>
    public enum EventPriority
    {
        Low = 1,
        Normal = 3,
        High = 7,
        Critical = 10
    }
}